package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;
import online.sh.cn.fy.common.utils.WxUtils;

/**
 * 编辑用户信息
 */
@WebServlet("/useredit")
public class UserEditServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		try {
			openid=DES3Util.decrypt(openid);
		} catch (Exception e) {
			log(e.getMessage());
		}
//		openid=CookieUtil.decodeCookie(openid);
		if (null!=openid && openid!="") {
			TokenUtils to=new TokenUtils();
			JSONObject json=new JSONObject();
			List school=to.checkSchoolAll();
			String schoolresult="";
			int changecounts=to.CountProductByOpenid(openid);
			if(null!=school && school.size()!=0){
				for (int i = 0; i < school.size(); i++) {
					schoolresult+="<option value='"+school.get(i)+"'>"+school.get(i)+"</option>";
				}
			}else{
				schoolresult="";
			}
			Map user=to.getFyUserinfoMap(openid);
			List buildinglist=to.checkFloorBySchool(user.get("school").toString());
			String buildresult="";
			if(null!=buildinglist && buildinglist.size()!=0){
				for (int i = 0; i < buildinglist.size(); i++) {
					buildresult+="<option value='"+buildinglist.get(i)+"'>"+buildinglist.get(i)+"</option>";
				}
			}else{
				buildresult="";
			}
			String bandSchool=to.getSchoolBand(user.get("school").toString());
			if(null!=bandSchool && bandSchool!=""){
				json.put("school", user.get("school").toString());
			}else{
				json.put("school", "");
			}
			json.put("result", 0);
			json.put("changecounts", changecounts);
			json.put("nickname", user.get("nickname").toString());
			json.put("headimg", user.get("headimg").toString());
			json.put("name", user.get("name").toString());
			json.put("points", user.get("points").toString());
			json.put("schoollist", schoolresult);
			json.put("buildinglist",buildresult);
			json.put("building", user.get("building").toString());
			json.put("grade", user.get("grade").toString());
			json.put("major", user.get("major").toString());
			json.put("mobile", user.get("mobile").toString());
			ResponseUtils.renderJson(response, json.toString());		
		}else{
			JSONObject json=new JSONObject();
			json.put("result", 1);
			ResponseUtils.renderJson(response, json.toString());
		}
	}
	
	public static String changeTime(String time){
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		long ntime=new Long(time);  
		String d = format.format(ntime*1000);
		return d;
	}

}
